//
//  VideoDao.swift
//  DownloadTube
//
//  Created by Carlos on 28/10/14.
//  Copyright (c) 2014 Noname. All rights reserved.
//

import UIKit
import CoreData

class VideoDao: BaseDao {
 
    class var sharedInstance: VideoDao {
        struct Static {
            static var instance: VideoDao?
            static var token: dispatch_once_t = 0
        }
        
        dispatch_once(&Static.token) {
            Static.instance = VideoDao()
        }
        
        return Static.instance!
    }
    
    
    func findByTitleOrAuthorOrderByTitleAsc(filter: String) -> [AnyObject]{
     
        return findByTitleOrAuthor(filter, sortDescriptor: nil)
    }
    
    func findByTitleOrAuthorOrderByLengthDesc(filter: String) -> [AnyObject]{
        
        let sortDescriptor = NSSortDescriptor(key: "length", ascending: false)
        
        return findByTitleOrAuthor(filter, sortDescriptor: sortDescriptor)
    }
    
    func findByTitleOrAuthorOrderBySizeDesc(filter: String) -> [AnyObject]{
        
        let sortDescriptor = NSSortDescriptor(key: "spaceOnDisk", ascending: false)
        
        return findByTitleOrAuthor(filter, sortDescriptor: sortDescriptor)
    }
    
    func findByTitleOrAuthorOrderBySavedDateDesc(filter: String) -> [AnyObject]{
        
        let sortDescriptor = NSSortDescriptor(key: "downloadDate", ascending: false)
        
        return findByTitleOrAuthor(filter, sortDescriptor: sortDescriptor)
    }
    
    func findByTitleOrAuthorOrderByAuthorAsc(filter: String) -> [AnyObject]{
        
        let sortDescriptor = NSSortDescriptor(key: "author", ascending: true, selector: "caseInsensitiveCompare:")
        
        return findByTitleOrAuthor(filter, sortDescriptor: sortDescriptor)
    }
    
    func findByTitleOrAuthor(filter: String, var sortDescriptor: NSSortDescriptor?) -> [AnyObject]{
        
        if(sortDescriptor == nil){
            
            sortDescriptor = NSSortDescriptor(key: "title", ascending: true, selector: "caseInsensitiveCompare:")
        }
        
        let fetchRequest = NSFetchRequest(entityName: "Video")
        fetchRequest.returnsObjectsAsFaults = false
        fetchRequest.sortDescriptors = [sortDescriptor!]
        
        if(countElements(filter) > 0) {
            
            let predicate = NSPredicate(format: "author contains[cd] %@ OR title contains[cd] %@", filter, filter)
            fetchRequest.predicate = predicate
        }
        
        var results = managedObjectContext().executeFetchRequest(fetchRequest, error: nil)!
        return results
    }
    
    
    func saveVideo(videoId:String, title:String, author:String, filename:String, thumbnailFilename:String, type:String, downloadDate:NSDate, spaceOnDisk:Int64, length:String) -> NSError?{
        
        let context = managedObjectContext()
        let videoMO = NSEntityDescription.insertNewObjectForEntityForName("Video", inManagedObjectContext: context) as VideoMO
        var error:NSError?
        
        videoMO.videoId = videoId
        videoMO.title = title
        videoMO.author = author
        videoMO.filename = filename
        videoMO.thumbnailFilename = thumbnailFilename
        videoMO.type = type
        videoMO.downloadDate = NSDate()
        videoMO.spaceOnDisk = spaceOnDisk
        videoMO.length = length
  
        context.save(&error)
        
        
        println(videoMO)
        return error
    }
   }
